
* This code replicates the two figures in "Holding the World Together? The Future of Territorial Order"


***************
* Figure 1
***************

* this code uses two files:
* fig1data_conquests.dta contains the cases of forcible territorial change
* fig1data_dyads.dta in annual data on the number of contiguous dyads per year

use fig1data_conquests, clear

egen nconquest=count(year),by(year)
egen nisland=sum(islands),by(year)

duplicates drop year, force
keep year nconquest nisland
sort year

merge 1:1 year using fig1data_dyads
drop _merge
recode nconquest .=0
recode nisland .=0

* generate rate per year
gen perconquest=nconquest/ncontig

* aggregrate to decades
gen decade=int(year/10)*10
foreach v in nconquest nisland  {

	egen `v'_decade=sum(`v'),by(decade)
	
	}
	
egen perconquest_decade=mean(perconquest),by(decade)

duplicates drop decade, force
sort decade

* make figure

label var nconquest_decade "Total"
label var nisland_decade "Islands"
label var perconquest_decade "Rate per contiguous dyad"
label var decade "Decade"

twoway (bar nconquest_decade decade, barwidth(10)) (bar nisland_decade decade, barwidth(10)) (line perconquest_decade decade, lcolor(black) yaxis(2)), ///
	ytitle(, axis(2) margin(medium) orient(rvertical)) legend(cols(3) region(lcolor(white))) ytitle("Count", axis(1) margin(medium)) ylabels(,axis(2) angle(rvertical))



***************
* Figure 2
***************

	
* lowess on effects of time
local i=1
foreach v in borderage peacedur postdispdur {

	use fig2data, clear
	
	lowess disponset `v', gen(lowess_`v') nograph
	
	* the generated variable will be missing for observations in which disponset is missing due to ongoing dispute
	* drop those cases so that the generated value is kept when the data are collapsed by time
	
	drop if disponset==.
	
	* now collapse to one observation per time increment
	duplicates drop `v', force
	keep `v' lowess_`v'
	rename `v' time
	sort time
	
	if `i'>1 {
	
		merge 1:1 time using temp
		drop _merge
	
		}
		
	save temp, replace
	local i=`i'+1

	}


label var lowess_borderage "Age of dyad"
label var lowess_peacedur "Peace duration"
label var lowess_postdispdur "Years since last dispute"
label var time "Years"

sort time

graph set window fontface "Times New Roman"		
line lowess_borderage lowess_peacedur lowess_postdispdur  time if time<101, ///
	scheme(s2mono) graphregion(color(white)) legend(cols(3) region(lcolor(white))) ytitle("Probability of Dispute Onset", margin(medium))
	